define(
		[ "PDUtilDir/grid", "PDUtilDir/dataService", "PDUtilDir/util",
            "AppPathDir/product/productInfos/addTimetable",
            "AppPathDir/product/productInfos/updateTimetable", "AppPathDir/order/orderForVisitorProductInfos/add", "PDUtilDir/searchBlock",
				 "Date", "DateCN", "css!DateCss" ],
		function( Grid, DataService, Util, AddSupport, UpdateSupport, AgencySupport, SearchBlock) {
			HomeApp.controller('BodyCtrl',["$scope", "$compile", '$sce', function($scope, $compile, $sce) {
				$scope.entity = {
                    product: DataService.getObject('product.object')
				};
				$scope.search = {};
                $scope.cache = {};

                // SearchBlock.init('searchBlockIdForTimetable');

                //出发日期初始化
                $("#dateSearch").datetimepicker({
                    // 设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
                    language : "cn",
                    // 输出格式化
                    // format : 'yyyy-mm-dd hh:ii:ss',
                    format : 'yyyy-mm-dd',
                    // 直接选择‘今天’
                    todayBtn : true,
                    // 设置最精确的时间选择视图
                    minView : 'month',
                    // 高亮当天日期
                    todayHighlight : true,
                    // 显示“上午”“下午”
                    showMeridian : true,
                    // 选择完毕后自动关闭
                    autoclose : true
                });

                // 添加
                $scope.addSupport = AddSupport($scope, $compile);

                // 修改
                $scope.updateSupport = UpdateSupport($scope, $compile);

                // 删除
                $scope.delSupport = {
                    run : function(id) {
                        Util.confirm("是否要删除", function() {
                            $.ajax({
                                url : getServer() + "/flea/hclv/product/deleteTableById.htmls",
                                method : 'post',
                                data : {
                                    'model' : JSON.stringify({
                                        productId: $scope.entity.product.id,
                                        timeTableId: id
                                    })
                                },
                                success : function(data) {
                                    if(data.status != 200){
                                        return ;
                                    }
                                    $scope.grid.refresh();
                                    Util.alert(data.message);
                                }
                            });
                        });
                    }
                };
                $scope.searchSupport = {
                    run : function() {
                        $scope.grid.reload({
                            queryParam : {
                                model: JSON.stringify($scope.search)
                            }
                        })
                    }
                };
                //导出
                $scope.exportFun = function(id) {
                    Util.confirm("确认导出？", function() {
                        var url = getServer() + "/flea/hclv/car/visitor/exportVisitorSeats.htmls?timeTableId=" + id;
                        form = $("<form></form>").attr("action", url).attr("method", "post");
                        form.appendTo('body').submit().remove();
                    });
                };
                //代客下单
                $scope.agencySupport = AgencySupport($scope, $compile);
                // 删除
                $scope.delSupport = {
                    run : function() {
                        var selectRows = $scope.grid.getSelectedRow();
                        if (selectRows.length == 0) {
                            Util.alert('请选择至少一条的数据！');
                            return;
                        }
                        Util.confirm("是否要删除", function() {
                            var ids = new Array();
                            $.each(selectRows, function(index, selectRow) {
                                ids.push(selectRow.id);
                            });
                            $.ajax({
                                url : getServer() + "/flea/hclv/product/deleteTableByIds.htmls",
                                method : 'post',
                                data : {
                                    'model' : JSON.stringify({
                                        productId: $scope.entity.product.id,
                                        timeTableIds: ids
                                    })
                                },
                                success : function(data) {
                                    if(data.status != 200){
                                        return ;
                                    }
                                    $scope.grid.refresh();
                                    Util.alert(data.message);
                                }
                            });
                        });
                    }
                };

                // SearchBlock.init('searchBlockIdForTimetable');
                $scope.grid = Grid({
                    id : "ProductTimtableGirdDivId",
                    pageSize : 10,
                    multi : true,
                    pagination : true,
                    cache : false,
                    layout : [ {
                        name : "时间",
                        field : "date",
                        width: '100px',
                        sort : true,
                        format: function(e){
                            return e.row.date.replace("00:00:00", "");
                        },
                        click : function(e) {
                            $scope.updateSupport.run(e.data.row);
                        }
                    }, {
                        name : "数量",
                        field : "personNum",
                        width: '60px'
                    }, {
                        name : "余量",
                        field : "restPersonNum",
                        width: '60px'
                    }, {
                        name : "成人成本",
                        field : "costPrice",
                        width: '80px',
                        click : function(e) {
                            $scope.updateSupport.run(e.data.row);
                        }
                    }, {
                        name : "成人价格",
                        width: '80px',
                        field : "salePriceAdult"
                    }, {
                        name : "儿童成本",
                        width: '80px',
                        field : "costPriceChild"
                    }, {
                        name : "儿童价格",
                            width: '80px',
                        field : "salePriceChild"
                    }, {
                        name : "其他成本",
                        width: '80px',
                        field : "costPriceOther"
                    }, {
                        name : "其他价格",
                        width: '80px',
                        field : "salePriceOther"
                    }, {
                        name : "创建人",
                        field : "createUserName"
                    }, {
                        name : "创建时间",
                        width: '140px',
                        field : "createTime"
                    }, {
                        name : "操作",
                        field : "id",
//						width: '60px',
                        format : function(e) {
                            return '<a href="javascript:angular.element(\'#ProductTimtableGirdDivId\').scope().exportFun(\'' + e.row.id + '\')">导出</a>'
                                    + '<a href="javascript:angular.element(\'#ProductTimtableGirdDivId\').scope().delSupport.run(\'' + e.row.id + '\')">删除</a>'
                                + '<a href="javascript:angular.element(\'#ProductTimtableGirdDivId\').scope().agencySupport.run(\'' + e.row.id + '\')">代客下单</a>';
                        }
                    } ],
                    toolbar : [ {
                        name : "添加",
                        icon : "fa fa-plus-circle",
                        callback : function(event) {
                            $scope.addSupport.run();
                        }
                    }, {
                        name : "查询",
                        icon : "fa fa-search",
                        callback : function(event) {
                            $scope.searchSupport.run();
                        }
                    }, {
                        name : "删除",
                        icon : "fa fa-trash-o",
                        callback : function(event) {
                            $scope.delSupport.run();
                        }
                    } ],
                    data : {
                        type : 'URL',
                        value : getServer()
                        + '/flea/hclv/product/findTimeTableByProductId.htmls?model=' + $scope.entity.product.id
                    }
                });
			}
	]);
});